﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace common.controls
{
    public partial class baseListEdit : baseUserControl
    {
        public baseListEdit()
        {
            try
            {
                InitializeComponent();
                //SetData(myDataTbl);
                //detailGrid.DisableReadOnlyColumn = false; 
            }
            catch (Exception er)
            {
                ErrorHandler(this, er); 
            }            
        }
        public override void SetLanguage()
        {
            base.SetLanguage();
            addLineBtn.Text = language.GetString("add");
            deleteLineBtn.Text = language.GetString("delete");
        }

        public virtual void Init()
        {
            detailGrid.Location = new Point(0, addLineBtn.Location.Y + addLineBtn.Height+5);
            detailGrid.Size = new Size(this.Width, this.Height - detailGrid.Location.Y); 
        }
        public virtual void LoadData(string emCode) {}
        public virtual void SaveData(string emCode){}

        private bool _fReadOnly = false;
        public bool ReadOnly
        {
            get {return _fReadOnly;}
            set
            {
                _fReadOnly = value;
                if (_fReadOnly)
                {
                    addLineBtn.Enabled = false;
                    deleteLineBtn.Enabled = false;
                }
            }
        }
        public virtual void LockEdit(bool state)
        {
            addLineBtn.Enabled = !state;
            deleteLineBtn.Enabled = !state;
        }
        public virtual bool CheckData() {return false;}
        public virtual void SetFocus(){}
        public virtual void AddNew()
        {
            gridDataSource.AddNew();
        }

        protected virtual void CurrentDataChanged(){}
        private void gridDataSource_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                CurrentDataChanged();
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }

        private void addLineBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gridDataSource.Current != null && !CheckData())
                {
                    SysLibs.ShowErrorMessage(String.Format(language.GetString("invalidData")));
                    return;
                }
                AddNew();
                LockEdit(detailGrid.RowCount == 0);
                addLineBtn.Enabled = true;
                SetFocus();
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }
        private void deleteLineBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.detailGrid.CurrentRow == null) return;
                this.detailGrid.Rows.RemoveAt(detailGrid.CurrentRow.Index);
                CheckData();
                LockEdit(detailGrid.RowCount == 0);
                addLineBtn.Enabled = true;
            }
            catch (Exception er)
            {
                ErrorHandler(sender,er);
            }
        }

        private void detailGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            ErrorHandler(sender, e.Exception);
        }
    }
}
